package view;

import java.util.Observable;
import java.util.Observer;

import model.OnScreen;
import model.Snippet;
import model.Dimension;
import model.Coordinate;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class SnippetView extends View implements Observer 
{
	private ScannerActivity _context;
	private Paint _paint;
	private Path _path;
	private Camera _camera;
	
	public SnippetView(Context context, Snippet newSnippet) 
	{
		super(context);
		
		_context = (ScannerActivity) context;
		setLayoutParams(new LayoutParams(0,0));
		
//		setColor(newSnippet.isInformation());
		//setLocation(newSnippet.getCoordinate(), newSnippet.getDimension());
		LayoutParams params = getLayoutParams();
		params.height = OnScreen.getInstance().getScreenDimension().getHeight();
		params.width = OnScreen.getInstance().getScreenDimension().getWidth();
		setLayoutParams(params);
		prepareOnDrawVars(newSnippet);
		

		
		
		setWillNotDraw(false);
	}
	
	@SuppressLint({ "NewApi"})
	private void setLocation(Coordinate coord, Dimension dimension)
	{
		try {
		Log.d("SNIPVIEW", "setlocation --- " + getLayoutParams().toString());
		setX(coord.getX() - (dimension.getWidth() / 2));
		setY(coord.getY() - (dimension.getHeight() / 2));
		LayoutParams params = getLayoutParams();
		params.height = dimension.getHeight();
		params.width = dimension.getWidth();
		setLayoutParams(params);
		} catch (Exception e) {
			Log.d("SNIPVIEW", "setlocationERROR" + e.toString());
		}
	}
	
//	private void setColor(boolean status)
//	{
//		int background = status ? R.drawable.green : R.drawable.red;
//		
//		setBackgroundResource(background);
//	}

	@Override
	public void update(Observable observable, Object data) 
	{
		Log.d("SNIPVIEW", String.format("Updating - %s", data));
		// TODO Auto-generated method stub
		
		/*
		 * possible incoming updates:
		 * - string with the word "DELETED"
		 * - updated snippet object
		 */

		Snippet snippet = null;
		try {
			snippet = (Snippet) observable;
		} catch (ClassCastException e) 
		{
			Log.e("OBSERVER", String.format("%s", e.toString()));
		}
		 
		
		
		if(data != null && ((String)data).equals("DELETED"))
		{
			try {
			Log.d("SNIPVIEW", "LALALALALA WEG" + snippet.getID());
			
			_context.removeSnippetFromView(this);
			
			return;
			} catch( Exception e) {
				Log.d("SNIPVIEW", "mainLayout = null " + e.toString());
			}
		}
		
		if(snippet != null)
		{
			//setColor(snippet.isInformation());
			//setLocation(snippet.getCoordinate(), snippet.getDimension());
			
			Log.d("SNIPVIEW", snippet.toString());
		}
		
		
	}
	
	private void prepareOnDrawVars(Snippet snippet)
	{
		schaalVanLinkerCodenaarRechterCode = OnScreen.getInstance().getLeftTorightCodeScale();
		x = snippet.getCoordinate().getX();
		y = snippet.getCoordinate().getY();
		width = snippet.getDimension().getWidth();
		height = snippet.getDimension().getHeight();
		_status = snippet.isInformation();
		
		_paint = new Paint();
		_camera = new Camera();
		_path = new Path();
		
	}
	
	private float schaalVanLinkerCodenaarRechterCode = 1f;
	private int x,y,width,height = 0;
	private boolean _status = false;
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		int color = _status ? Color.GREEN : Color.RED;
		_paint.setColor(color);
		_paint.setStyle(Paint.Style.FILL);
		
		_camera.save();
//		_camera.rotateY(angle);
		_camera.applyToCanvas(canvas);
		
		
		canvas.save();
		
//		versie vergroten		
//		if(schaalVanLinkerCodenaarRechterCode >= 1.0f)
//		{
//			double extraOnTheSides = ((height * schaalVanLinkerCodenaarRechterCode) - height) / 2;
//			path.moveTo(x,(float)(y-extraOnTheSides)); // startpoint 							(linksboven)
//			path.lineTo(x+width, y);		//													(rechtsboven)	
//			path.lineTo(x+width, y+height);//													(rechtsonder)
//			path.lineTo(x, (float)(y+height+extraOnTheSides));								//	(linksonder)
//			path.lineTo(x, (float)(y-extraOnTheSides)); // 										terug naar linksboven		
//		}
//		else
//		{
//			double extraOnTheSides = ((height * (1/schaalVanLinkerCodenaarRechterCode)) - height) / 2;
//			path.moveTo(x,y); // startpoint 													(linksboven)
//			path.lineTo(x+width, (float)(y-extraOnTheSides));		//							(rechtsboven)	
//			path.lineTo(x+width, (float)(y+height+extraOnTheSides));//							(rechtsonder)
//			path.lineTo(x, y+height);								//							(linksonder)
//			path.lineTo(x, y); // 																terug naar linksboven		
//		}

//		versie verkleinen
		if(schaalVanLinkerCodenaarRechterCode >= 1.0f)
		{
			double subtractFromY = ((height * schaalVanLinkerCodenaarRechterCode) - height) / 2;
			_path.moveTo(x,y); // startpoint 													(linksboven)
			_path.lineTo(x+width, (float)(y+subtractFromY));		//								(rechtsboven)	
			_path.lineTo(x+width, (float)(y+height-subtractFromY));//							(rechtsonder)
			_path.lineTo(x, y+height);														//	(linksonder)
			_path.lineTo(x, y); // 																terug naar linksboven		
		}
		else
		{
			double subtractFromY = ((height * (1/schaalVanLinkerCodenaarRechterCode)) - height) / 2;
			_path.moveTo(x,(float)(y+subtractFromY)); // startpoint 								(linksboven)
			_path.lineTo(x+width, y);		//													(rechtsboven)	
			_path.lineTo(x+width, (float)(y+height));//											(rechtsonder)
			_path.lineTo(x, (float)(y+height-subtractFromY));//									(linksonder)
			_path.lineTo(x, (float)(y+subtractFromY)); // 										terug naar linksboven		
		}		
		
		
		
		canvas.drawPath(_path,_paint);
		
		
		canvas.restore();
	}
	
}